package com.sxt.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.TreeNode;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.mapper.PermissionMapper;
import com.sxt.sys.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sxt.sys.vo.PermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老雷
 * @since 2019-09-21
 */
@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

	
	@Override
	public boolean removeById(Serializable id) {
		PermissionMapper permissionMapper = this.getBaseMapper();
		//根据权限或菜单ID删除权限表各和角色的关系表里面的数据
		permissionMapper.deleteRolePermissionByPid(id);
		return super.removeById(id);//删除 权限表的数据
	}

	/**************** 权限管理开始 ****************/

	/**
	 * 加载权限管理左边的权限树的json
	 */
	public List<TreeNode> loadPermissionManagerLeftTreeJson(PermissionVo permissionVo) {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type", Constast.TYPE_MNEU);
		List<Permission> list = this.list(queryWrapper);
		List<TreeNode> treeNodes = new ArrayList<>();
		for (Permission permission : list) {
			Boolean spread = permission.getOpen() == 1 ? true : false;
			treeNodes.add(new TreeNode(permission.getId(), permission.getPid(), permission.getTitle(), spread));
		}
		return treeNodes;
	}

	/**
	 * 查询
	 */
	public IPage<Permission> loadAllPermission(PermissionVo permissionVo) {
		IPage<Permission> page = new Page<>(permissionVo.getPage(), permissionVo.getLimit());
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type", Constast.TYPE_PERMISSION);// 只能查询权限
		queryWrapper.like(StringUtils.isNotBlank(permissionVo.getTitle()), "title", permissionVo.getTitle());
		queryWrapper.like(StringUtils.isNotBlank(permissionVo.getPercode()), "percode", permissionVo.getPercode());
		queryWrapper.eq(permissionVo.getId() != null,
				"pid", permissionVo.getId());
		queryWrapper.orderByAsc("ordernum");
		this.page(page, queryWrapper);
		return page;
	}

	/**
	 * 加载最大的排序码
	 *
	 * @param
	 */
	public List<Permission> loadPermissionMaxOrderNum() {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("ordernum");
		IPage<Permission> page = new Page<>(1, 1);
		List<Permission> list = this.page(page, queryWrapper).getRecords();
		return list;
	}

	/**
	 * 添加
	 *
	 * @param permissionVo
	 * @return
	 */
	public Boolean addPermission(PermissionVo permissionVo) {
		try {
			permissionVo.setType(Constast.TYPE_PERMISSION);// 设置添加类型
			this.save(permissionVo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 修改
	 *
	 * @param permissionVo
	 * @return
	 */
	public Boolean updatePermission(PermissionVo permissionVo) {
		try {
			this.updateById(permissionVo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 删除
	 *
	 * @param permissionVo
	 * @return
	 */
	public Boolean deletePermission(PermissionVo permissionVo) {
		try {
			this.removeById(permissionVo.getId());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**************** 权限管理结束 ****************/


}
